#include "../comm/httplib.h"
#include <iostream>
#include "oj_control.hpp"
using namespace httplib;
using namespace ns_control;

int main()
{
    Control ctrl;
    Server ser;
    // 获取所有题目列表
    ser.Get("/all_questions", [&ctrl](const Request &req, Response &resp)
            {
                string html;
                ctrl.AllQuestions(&html);
                resp.set_content(html, "text/html; charset=utf-8"); });
    // 用户要根据题目编号，获取题目的内容
    // /question/100 -> 正则匹配 --question文件夹下的题目号
    // R"()", 原始字符串raw string,保持字符串内容的原貌，不用做相关的转义
    ser.Get(R"(/question/(\d+))", [&ctrl](const Request &req, Response &resp)
            {
                string html;
                std::string number = req.matches[1];
                ctrl.Question(number, &html);
                resp.set_content(html, "text/html; charset=utf-8"); });

    // 根据题目编号进行判题
    ser.Post(R"(/judge/(\d+))", [&ctrl](const Request &req, Response &resp)
            {
                std::string number = req.matches[1];
                std::string result_json;
                ctrl.Judge(number,req.body,&result_json); 
                resp.set_content(result_json, "application/json;charset=utf-8");
                });
            

    ser.set_base_dir("./wwwroot");
    ser.listen("0.0.0.0", 8080);
}